
import { _decorator, Component, Node, systemEvent, SystemEventType, RigidBody, Vec3, Prefab, instantiate, director, Label, random, Camera, EventKeyboard, macro } from 'cc';
import { Ball } from './ball';
import { Wall } from './wall';
const { ccclass, property } = _decorator;

@ccclass('Game')
export class Game extends Component {
    // [1]
    // dummy = '';

    // [2]
    // @property
    // serializableDummy = 0;

    // 小球
    @property(Node)
    private ballNode: Node = null!;

    // 跳板预制体
    @property(Prefab)
    private blockPre: Prefab = null!

    // 分数控件
    @property(Label)
    private score: Label = null!

    // 主摄像头。2d界面
    @property(Camera)
    private Camera0: Camera = null!

    // 3d界面
    @property(Camera)
    private Camera1: Camera = null!

    // 跳板数组
    private blockArr: Node[] = []

    // 最后一个加载的跳板
    private lastBlock: Node = null!

    // 0:等待；1：开始；2：结束
    private gameState: number = 0

    // 分数
    private scoreN: number = 0

    start() {
        // [3]
        // systemEvent.on(SystemEventType.TOUCH_START, this.onTouchStart, this);
        // systemEvent.on(SystemEventType.KEY_DOWN,this.onTouchStart,this);
        systemEvent.on(SystemEventType.KEY_DOWN, this.onKeyDown, this);
        this.initBlock()
    }

    // 触摸函数
    onTouchStart() {
        console.log("加速")
        // 获取ball对象
        let ball = this.ballNode.getComponent("Ball") as Ball;
        // 调用函数
        ball.boost();
        // 修改状态：开始
        if (this.gameState == 0) {
            console.log("开始")
            this.gameState = 1;
        }
    }

    // 按键函数
    onKeyDown(e: EventKeyboard) {

        switch (e.keyCode) {
            case macro.KEY.enter:
                this.changeCamera();
                break;
            case macro.KEY.space:
                this.onTouchStart()
                break;
            default:
                break;
        }

    }

    // 切换摄像头
    changeCamera() {
        if (this.Camera0.node.active) {
            this.Camera0.node.active = false;
            this.Camera1.node.active = true;
        }
        else {
            this.Camera1.node.active = false;
            this.Camera0.node.active = true;
        }

    }

    // 跳板初始化
    initBlock() {
        // 生成5个
        for (let index = 0; index < 5; index++) {
            // 初始化预制体
            let blockN = instantiate(this.blockPre)
            // 添加预制体到当前控件节点
            this.node.addChild(blockN)
            // 添加到跳板数组
            this.blockArr.push(blockN)
            // 设置坐标
            blockN.setPosition(index * 2, 0, 0)
        }
        // 从跳板数组获取最后一个预制体
        this.lastBlock = this.blockArr[this.blockArr.length - 1]
    }

    // 动态显示分数
    initScore(num: number) {
        this.scoreN += num;
        this.score.string = this.scoreN.toString();
    }

    update(deltaTime: number) {
        // [4]
        // 状态为“开始”，就开启刷新
        if (this.gameState == 1) {
            // 小球掉出界面就结束
            if (this.ballNode.getPosition().y < -4) {
                console.log("结束")
                this.gameState = 2;
                director.loadScene("main")
                return;
            }

            // 设置跳板移动速度。根据刷新速率决定单次刷新的移动距离，每秒总共向左移动二个像素
            let speed = -2 * deltaTime;

            // 遍历跳板数组
            for (let blockN of this.blockArr) {
                // 获取单个跳板
                let nowPos = blockN.getPosition()
                // 下一次移动到的x坐标
                let nextX = nowPos.x += speed;

                // 跳板移动到屏幕外
                if (nextX <= -4) {
                    // 分数加一
                    this.initScore(1);
                    // 移动到最后一个跳板之后
                    blockN.setPosition(this.lastBlock.getPosition().x + 2, nowPos.y, nowPos.z);
                    // 成为最后一个跳板
                    this.lastBlock = blockN
                    // 重新初始化
                    let block = blockN.getComponent('Wall') as Wall;
                    block.start();
                } else {
                    // 跳板向左移动
                    blockN.setPosition(nextX, nowPos.y, nowPos.z)
                }
            }

        }
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.0/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.0/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.0/manual/en/scripting/life-cycle-callbacks.html
 */
